<script>
import ValidCode from "@/components/ValidCode";

export default {
  name: "login",
  components: {
    ValidCode
  },
  data() {
    const validateCode = (rule, value, callback) => {
      if (value.toLowerCase() !== this.code.toLowerCase()) {
        return callback(new Error('验证码不正确'))
      } else if (value === '') {
        return callback(new Error('验证码不能为空'))
      }
      callback()
    }
    return {
      code: '',  // 验证码组件绑定的值
      forgetPwdDialogVis: false,
      forgetPwdForm: {},
      user: {
        username: '',
        password: '',
        code: '', // 用户输入的验证码
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
          {min: 5, max: 10, message: '长度在 5 到 10 个字符', trigger: 'blur'}
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          {min: 6, max: 14, message: '长度在 6 到 14 个字符', trigger: 'blur'}
        ],
        code: [
          {validator: validateCode, trigger: 'blur'}
        ]
      }
    }
  },
  methods: {
    login() {
      this.$refs.loginRef.validate(valid => {
        if (valid) {
          this.$request.post("/user/login", this.user).then(res => {
            console.log(res)
            if (res.code === "200") {
              this.$message.success("欢迎用户! " + res.data.username)
              this.$router.push('/')
              localStorage.setItem('honey-user', JSON.stringify(res.data)) // 存储用户数据
            } else {
              this.$message.error(res.msg) //接收后端异常发送的信息
            }
          })
        }
      })

    },
    getCode(code) {
      console.log(code)
      this.$emit('update:value', '')
      this.code = code
    },
    resetPwd() {
      this.$request.put('/user/resetPwd', this.forgetPwdForm).then(res => {
        if (res.code === '200') {
          this.$message.success('重置密码成功')
          this.forgetPwdDialogVis = false
        } else {
          this.$message.error(res.msg)
        }
      })
    },
    handleForgetPwd() {
      this.forgetPwdDialogVis = true
      this.forgetPwdForm = {}
    }
  }
}
</script>

<template>
  <div class="login">
    <div class="box">
      <div style="flex:1">
        <img src="@/assets/login.jpg" alt="" style="width: 500px; height: 500px; background-size: cover;
        background-position: center;background-repeat: no-repeat">
      </div>
      <div style="flex:1;display: flex; align-items: center;justify-content: center">
        <el-form :model="user" style="width: 80%" :rules="rules" ref="loginRef">
          <div class="title">欢迎登录后台管理系统</div>
          <el-form-item prop="username">
            <el-input prefix-icon="el-icon-user" size="large" id="username" placeholder="请输入用户名"
                      v-model="user.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input prefix-icon="el-icon-lock" size="large" show-password id="password" placeholder="请输入密码"
                      v-model="user.password"></el-input>
          </el-form-item>
          <el-form-item prop="code">
            <div style="display: flex;">
              <el-input prefix-icon="el-icon-tickets" size="large" style="flex: 1" placeholder="请输入验证码"
                        v-model="user.code">
              </el-input>
              <div style="flex: 1;width: 190px;height: 36px; user-select: none">
                <valid-code @update:value="getCode"/>
              </div>
            </div>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" plain size="medium" style="width: 100%" @click="login">登录</el-button>
          </el-form-item>
          <div style="display: flex;">
            <div style="flex: 1">
              <span>还没有账号？请</span>
              <span style="color: #0f9876;cursor: pointer" @click="$router.push('/register')">点击注册</span>
            </div>
            <div style="flex: 1;text-align: right;color: #0f6898;cursor: pointer" @click="handleForgetPwd">忘记密码
            </div>
          </div>
        </el-form>
      </div>
    </div>
    <el-dialog title="忘记密码" :visible.sync="forgetPwdDialogVis" width="30%">
      <el-form :model="forgetPwdForm" label-width="80px" style="padding-left: 20px">
        <el-form-item label="用户名">
          <el-input v-model="forgetPwdForm.username" autocomplete="off" placeholder="请输入用户名"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input v-model="forgetPwdForm.phone" autocomplete="off" placeholder="请输入手机号"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="forgetPwdDialogVis = false">取消</el-button>
        <el-button type="primary" @click="resetPwd">确定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<style scoped>
.login {
  height: 100vh;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #0f9876;
}

.box {
  display: flex;
  background-color: white;
  width: 60%;
  height: 500px;
  border-radius: 5px;
  overflow: hidden;
}

.title {
  font-size: 30px;
  color: #000;
  font-weight: bold;
  text-align: center;
  padding-bottom: 20px;
}

</style>